//
//  XFBubbleView.m
//  WorkManager
//
//  Created by ukongm on 17/2/28.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "XFBubbleView.h"
#import "XFManager.h"
/** 控件的高度 */
static CGFloat const VIEW_HEIGHT = 16;
@interface XFBubbleView()

/** 展示label */
@property(nonatomic,strong)UILabel* countLb;

@end

@implementation XFBubbleView

#pragma mark 类方法
+ (instancetype)bubble{
    return [[self alloc] init];
}

- (UILabel *)countLb{
    
    if (_countLb == nil) {
        _countLb = [[UILabel alloc] initWithFrame:self.bounds];
        _countLb.font = [UIFont boldSystemFontOfSize:13];
        _countLb.textColor = [UIColor whiteColor];
        _countLb.textAlignment = NSTextAlignmentCenter;
    }
    return _countLb;
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    frame = [self frameForFrame:frame];
    
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)setFrame:(CGRect)frame{
    frame = [self frameForFrame:frame];
    
    [super setFrame:frame];
    
    [self changeFrame];
}

- (CGRect)frameForFrame:(CGRect)frame{
    
    frame.size.height = VIEW_HEIGHT;
    if (frame.size.width < VIEW_HEIGHT) {
        frame.size.width = VIEW_HEIGHT;
    }
    return frame;
}

- (void)createView{
    UIImage* image = [UIImage imageNamed:@"icon_red_count_small"];
    image = [image stretchableImageWithLeftCapWidth:image.size.width/2 topCapHeight:image.size.height/2];
    self.image = image;
    self.hidden = YES;
    [self addSubview:self.countLb];
    
}

- (void)changeFrame{
    self.countLb.frame = self.bounds;
}

- (void)setCount:(NSInteger)count{
    NSString* countStr = XFIntegerToString(count);
    self.countLb.text = countStr;
    
    if (count <= 0) {
        self.hidden = YES;
    }
    else{
        self.hidden = NO;
        
        //计算大小
        CGSize size = XFMathSizeForString(countStr, CGSizeMake(CGFLOAT_MAX, VIEW_HEIGHT), self.countLb.font);
        
        switch (self.expandType) {
            case XFBubbleViewExpandTypeSize:
                [self setFrame:CGRectMake(self.x, self.y, size.width, VIEW_HEIGHT)];
                break;
            case XFBubbleViewExpandTypeOrigin:
            {
                //计算差值
                if (size.width < VIEW_HEIGHT) {
                    size.width = VIEW_HEIGHT;
                }
                
                if (size.height < VIEW_HEIGHT) {
                    size.height = VIEW_HEIGHT;
                }
                CGFloat width_scale = size.width - self.width;
                CGFloat height_scale = size.height - self.height;
                [self setFrame:CGRectMake(self.x - width_scale, self.y - height_scale, size.width, VIEW_HEIGHT)];
                break;
            }
            case XFBubbleViewExpandTypeCenter:
                [self changeSize:size];
                break;
                
            default:
                break;
        }
    }
}

@end
